//
//  VideoShowCell.m
//  suRead
//
//  Created by 师爱华 on 17/3/7.
//  Copyright © 2017年 bao. All rights reserved.
//

#import "VideoShowCell.h"
#import "UIImageView+WebCache.h"
#import "M_News.h"
#import "M_SpecialNews.h"
#import "AccountManager.h"
#import "NewCellManager.h"

@interface VideoShowCell()

@property (weak, nonatomic) IBOutlet UIImageView *photoImageView;
@property (weak, nonatomic) IBOutlet UIImageView *playImageView;

@property (weak, nonatomic) IBOutlet UIView *categoryView;

@property (weak, nonatomic) IBOutlet UILabel *labelTitle;

@property (weak, nonatomic) IBOutlet UILabel *timeLabel;


@property (weak, nonatomic) IBOutlet UIImageView *zanImageView;
@property (strong, nonatomic) IBOutlet UILabel *zanLable;

@property (weak, nonatomic) IBOutlet UIImageView *countImageView;
@property (weak, nonatomic) IBOutlet UILabel *countLabel;

@property (weak, nonatomic) IBOutlet UIView *viewCountContainer;
@property (weak, nonatomic) IBOutlet UIImageView *imageViewFlag;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *flagWidthConstraint;
@end


@implementation VideoShowCell

- (void)setFrame:(CGRect)frame {
    frame.origin.y += 5;
    frame.size.height -= 5;
    [super setFrame:frame];
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    if ( [[[UIDevice currentDevice]systemVersion] floatValue] >= 8.0) {
        self.layoutMargins = UIEdgeInsetsZero;
        self.preservesSuperviewLayoutMargins = NO;
        self.separatorInset = UIEdgeInsetsMake(0, 15.0f, 0, 15.0f);
    }
}

+ (CGFloat)heightWithTitle:(NSString *)title_
{
    CGFloat height = 5.0f;
    
    height += ([UIScreen mainScreen].bounds.size.width - 10.0f * 2) * ( 200.0f / (375.0f - 10.0f * 2) );
    
    height += 5.0f;
    
    CGRect rect = [title_ boundingRectWithSize:CGSizeMake([UIScreen mainScreen].bounds.size.width - 10.0f * 2, NSIntegerMax) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16.0f]} context:nil];
    
    height += rect.size.height + 2.0f;
    
    height += 5.0f;
    
    return height;
}


- (void)setNewsData:(id)newsData
{
    M_News *news = (M_News *)newsData;
    
    NSString *imageURL = news.f_thumbnail;
   
    [self.photoImageView sd_setImageWithURL:[NSURL URLWithString:imageURL] placeholderImage:[UIImage imageNamed:@"lazy"]];
   
    [self.photoImageView setContentScaleFactor:[[UIScreen mainScreen] scale]];
    self.photoImageView.contentMode =  UIViewContentModeScaleAspectFill;
    self.photoImageView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
    self.photoImageView.clipsToBounds  = YES;
    
    NSString *title = news.f_title;
    self.labelTitle.text = title;
    
    //已读未读
    if ([[NewCellManager shared] hasRead:news.f_id]) {
        self.labelTitle.textColor = [UIColor lightGrayColor];
    } else {
        self.labelTitle.textColor = [UIColor blackColor];
    }
    
    NSString *createTime = news.f_updateTime;
    self.timeLabel.text = createTime;
    
    
    /*
     置顶|99
     最新|1
     实发|2
     热点|3
     独家|4
     专题|5
     推广|6
     视频|7
     直播|8
     福利9
     */
    self.flagWidthConstraint.constant = 36.0f;
    NSString *flagName = @"";
    switch ([news.f_newsFlag intValue]) {
        case 99:
            flagName = @"news_flag_01";
            break;
        case 1:
            flagName = @"news_flag_02";
            break;
        case 2:
            flagName = @"news_flag_03";
            break;
        case 3:
            flagName = @"news_flag_04";
            break;
        case 4:
            flagName = @"news_flag_05";
            break;
        case 5:
            flagName = @"news_flag_06";
            break;
        case 6:
            flagName = @"news_flag_07";
            break;
        case 7:
            flagName = @"news_flag_08";
            break;
        case 8:
            flagName = @"news_flag_09";
            break;
        case 9:
            flagName = @"news_flag_10";
            break;
        default:
            flagName = @"icon_time";
            self.flagWidthConstraint.constant = 15.0f;
            break;
    }
    self.imageViewFlag.image = [UIImage imageNamed:flagName];
    
    if ([AccountManager sharedAccountManager].f_isShowViewCount) {
        self.viewCountContainer.hidden = NO;
        self.countLabel.text = news.f_views;
        [self.zanLable setText:[NSString stringWithFormat:@"%ld",news.f_square]];

    } else {
        self.viewCountContainer.hidden = YES;
    }
    
}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
